package cn.Y_Qiu.pet.util;

import cn.Y_Qiu.user.domain.LoginInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.test.context.web.WebAppConfiguration;
import org.springframework.util.StringUtils;
import org.springframework.web.context.WebApplicationContext;
import org.springframework.web.context.support.WebApplicationContextUtils;

import javax.servlet.http.HttpServletRequest;

public class LoginContext {

    public static LoginInfo getLoginInfo(HttpServletRequest request){
        String token = request.getHeader("token");
        if (!StringUtils.isEmpty(token)) {
            WebApplicationContext applicationContext =
                    WebApplicationContextUtils.getWebApplicationContext(request.getServletContext());
            RedisTemplate redisTemplate = (RedisTemplate)applicationContext.getBean("redisTemplate");
            Object loginInfotmp = redisTemplate.opsForValue().get(token);
            if ( loginInfotmp != null) {
                return (LoginInfo) loginInfotmp;
            }
        }
        return null;
    }
}
